package com.example.myproject.web;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.GetResponse;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/9/27.
 */
@RestController
public class MqController {

    @Autowired
    private org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory;

//    @Resource(name="secondAdmin")
   private RabbitAdmin rabbitAdmin;

    @RequestMapping("/message/list")
    public int list  (){
        ConnectionFactory factory = (ConnectionFactory) rabbitAdmin.getRabbitTemplate().getConnectionFactory();
        Connection conn = null;
        try {
            conn = (Connection) connectionFactory.createConnection();
            Channel channel =  conn.createChannel(false);
            GetResponse response = channel.basicGet("dead.queue", false);
            System.out.println("count:"+response.getMessageCount());
            channel.close();
            return response.getMessageCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  0;
    }

}
